﻿// -------------------------------------------------------------------------
//    @FileName         :    NFSnsDetailPartModule.cpp
//    @Author           :    gaoyi
//    @Date             :    2025-03-31
//    @Email			:    445267987@qq.com
//    @Module           :    NFSnsDetailPartModule
//
// -------------------------------------------------------------------------

#include "NFSnsDetailPartModule.h"

#include <NFSnsDetailPart.h>
#include <NFSnsRoleDetailService.h>

#include "NFSnsRoleDetail.h"

NFSnsDetailPartModule::NFSnsDetailPartModule(NFIPluginManager *p) : NFIDysServiceModule(p)
{

}

NFSnsDetailPartModule::~NFSnsDetailPartModule()
{
}

int NFSnsDetailPartModule::OnHandleClientMessage(uint32_t msgId, NFDataPackage &packet, uint64_t roleId, uint64_t param2)
{
    if (!m_pObjPluginManager->IsInited(NF_ST_SNS_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Sns Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Sns Server is Stopping, drop client msg:{}", packet.ToString());
        return -1;
    }

    NFSnsRoleDetail *pRoleDetail = NFSnsRoleDetailService::GetRoleDetail(roleId);
    if (pRoleDetail)
    {
        if (msgId < m_clientMsgToServiceMap.size() && m_clientMsgToServiceMap[msgId]  != 0)
        {
            NFSnsDetailPart* pPart = pRoleDetail->GetPart(m_clientMsgToServiceMap[msgId]);
            if (pPart)
            {
                return pPart->OnHandleClientMessage(msgId, packet);
            }
        }
        else {
            NFLogError(NF_LOG_DEFAULT, roleId, "msgId:{} can't handle, drop the msg", msgId, m_clientMsgToServiceMap[msgId]);
        }
    }
    else {
        NFLogError(NF_LOG_DEFAULT, roleId, "can' find the player role:{}", roleId);
    }
    return 0;
}

int NFSnsDetailPartModule::OnHandleServerMessage(uint32_t msgId, NFDataPackage &packet, uint64_t roleId, uint64_t param2)
{
    if (!m_pObjPluginManager->IsInited(NF_ST_SNS_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Sns Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Sns Server is Stopping, drop client msg:{}", packet.ToString());
        return -1;
    }

    NFSnsRoleDetail *pRoleDetail = NFSnsRoleDetailService::GetRoleDetail(roleId);
    if (pRoleDetail)
    {
        if (msgId < m_serverMsgToServiceMap.size() && m_serverMsgToServiceMap[msgId]  != 0)
        {
            NFSnsDetailPart* pPart = pRoleDetail->GetPart(m_serverMsgToServiceMap[msgId]);
            if (pPart)
            {
                return pPart->OnHandleServerMessage(msgId, packet);
            }
            else {
                NFLogError(NF_LOG_DEFAULT, roleId, "can't find part, msgId:{} partId:{}, drop the msg", msgId, m_serverMsgToServiceMap[msgId]);
            }
        }
        else {
            NFLogError(NF_LOG_DEFAULT, roleId, "msgId:{} can't handle, drop the msg", msgId);
        }
    }
    return 0;
}